package longrise.phone.com.itssnew.bean;

import android.content.Context;
import android.os.Handler;
import android.widget.Toast;

import com.longrise.LEAP.Base.IO.JSONSerializer;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.Global;
import com.longrise.android.util.IntenetUtil;
import com.longrise.android.util.PreferenceUtils;

import longrise.phone.com.itssnew.R;

import static android.provider.Telephony.Mms.Part.FILENAME;

/**
 * Created by chengsan on 17/7/19.
 */

public class JZSQGloble
{

    private static JZSQGloble jzsqGloble;
    //ServerUrl存放的Key
    private static final String SERVICEURLKEY = "SERVICEURLKEY";
    private String ServerUrl;
    //UpdateUrl存放的Key
    private static final String UPDATEURLKEY = "SERVICEURLKEY";
    private String UpdateUrl;
    //LeapAppName存放的Key
    private static final String LEAPAPPNAMEKEY = "SERVICEURLKEY";
    private String LeapAppName;
    //RootDir存放的Key
    private static final String ROOTDIRKEY = "SERVICEURLKEY";
    private String RootDir;
    //存放userInfoBean的Key
    private static final String USERINFOBEAN_KEY = "USERINFOBEANKEY";
    private EntityBean userInfoBean;//用户信息，有的项目过来的数据类型就是Bean对象
    private static final String USERFLAGKEY = "USERFLAGKEY";
    private String userflag;//用户名

    private String FileNameStirng = "SQJZManagerApp";
    private Handler handler;
    private Context context;

    private JZSQGloble(Context context)
    {
        handler = new Handler(context.getMainLooper());
        if(context != null)
        {
            this.context = context.getApplicationContext();
        }
        FileNameStirng = context.getString(R.string.sh_name);
        if(null == FileNameStirng || FileNameStirng.isEmpty())
        {
            FileNameStirng = "SQJZManagerApp";
        }

        //获取属性值
        getServerUrl();
        getUpdateUrl();
        getLeapAppName();
        getRootDir();
        getUserInfoBean();
    }

    public static JZSQGloble getInstance(Context context)
    {
        if(jzsqGloble == null)
        {
            synchronized (JZSQGloble.class)
            {
                jzsqGloble = new JZSQGloble(context);
            }
        }
        return jzsqGloble;
    }


    public String getServerUrl()
    {
        if(null == ServerUrl || ServerUrl.isEmpty())
        {
            if(context != null)
            {
                ServerUrl = PreferenceUtils.getPrefString(FILENAME,
                        Context.MODE_PRIVATE,context,SERVICEURLKEY,"");
            }
        }
        return ServerUrl;
    }

    public void setUserflag(String userflag)
    {
        if(null != context)
        {
            PreferenceUtils.setPrefString(FILENAME,
                    Context.MODE_PRIVATE,context,USERFLAGKEY,userflag);
        }
        this.userflag = userflag;
    }

    public String getUserflag()
    {
        if(null == userflag || userflag.isEmpty())
        {
            if(context != null)
            {
                userflag = PreferenceUtils.getPrefString(FILENAME,
                        Context.MODE_PRIVATE,context,USERFLAGKEY,"");
            }
        }
        return userflag;
    }

    public void setServerUrl(String serverUrl)
    {
        if(null != context)
        {
            PreferenceUtils.setPrefString(FILENAME,
                    Context.MODE_PRIVATE,context,SERVICEURLKEY,serverUrl);
        }
        ServerUrl = serverUrl;
    }

    public String getUpdateUrl()
    {
        if(null == UpdateUrl || UpdateUrl.isEmpty())
        {
            if(context != null)
            {
                UpdateUrl = PreferenceUtils.getPrefString(FILENAME,
                        Context.MODE_PRIVATE,context,UPDATEURLKEY,"");
            }
        }
        return UpdateUrl;
    }

    public void setUpdateUrl(String updateUrl)
    {
        if(null != context)
        {
            PreferenceUtils.setPrefString(FILENAME,
                    Context.MODE_PRIVATE,context,UPDATEURLKEY,updateUrl);
        }
        UpdateUrl = updateUrl;
    }

    public String getLeapAppName()
    {
        if(null == LeapAppName || LeapAppName.isEmpty())
        {
            if(context != null)
            {
                UpdateUrl = PreferenceUtils.getPrefString(FILENAME,
                        Context.MODE_PRIVATE,context,LEAPAPPNAMEKEY,"");
            }
        }
        return LeapAppName;
    }

    public void setLeapAppName(String leapAppName)
    {
        if(null != context)
        {
            PreferenceUtils.setPrefString(FILENAME,
                    Context.MODE_PRIVATE,context,LEAPAPPNAMEKEY,leapAppName);
        }
        LeapAppName = leapAppName;
    }

    public String getRootDir()
    {
        if(null == RootDir || RootDir.isEmpty())
        {
            if(context != null)
            {
                RootDir = PreferenceUtils.getPrefString(FILENAME,
                        Context.MODE_PRIVATE,context,ROOTDIRKEY,"");
            }
        }
        return RootDir;
    }

    public void setRootDir(String rootDir)
    {
        if(null != context)
        {
            PreferenceUtils.setPrefString(FILENAME,
                    Context.MODE_PRIVATE,context,ROOTDIRKEY,rootDir);
        }
        RootDir = rootDir;
    }

    /**
     * 获取用户信息
     * @return
     */
    public EntityBean getUserInfoBean()
    {
        if(null == userInfoBean)
        {
            String tempString = null;
            if(context != null)
            {
                tempString = PreferenceUtils.getPrefString(FILENAME,
                        Context.MODE_PRIVATE,context,USERINFOBEAN_KEY,"");
            }
            if(null != tempString && !tempString.isEmpty())
            {
                userInfoBean = (EntityBean) JSONSerializer.getInstance().DeSerialize(tempString,
                        EntityBean.class);
            }
        }
        return userInfoBean;
    }

    /**
     * 设置用户信息
     * @param userInfoBean
     */
    public void setUserInfoBean(EntityBean userInfoBean)
    {
        if(null != context)
        {
            String string = "";
            if(null != userInfoBean)
            {
                string = JSONSerializer.getInstance().Serialize(userInfoBean);
            }
            PreferenceUtils.setPrefString(FILENAME,
                    Context.MODE_PRIVATE,context,USERINFOBEAN_KEY,string);
        }
        this.userInfoBean = userInfoBean;
    }

    /**
     * 调用接口
     * @param server
     * @param classType
     * @param parameters
     * @return
     */
    public Object call(final String server, final Class<?> classType, final OnCallFinishListener onCallFinishListener,final Object... parameters)
    {
        if(IntenetUtil.getNetworkState(context) == IntenetUtil.NETWORN_NONE)
        {
            Toast.makeText(context,"没有网络连接",Toast.LENGTH_SHORT).show();
            if(null != onCallFinishListener)
            {
                onCallFinishListener.onCallFinish(null);
            }
            return null;
        }
        new Thread(new Runnable() {
            @Override
            public void run() {

                if(null == Global.getInstance().getUserflag() || Global.getInstance().getUserflag().isEmpty())
                {
                    Global.getInstance().setUserflag(getUserflag());
                }
                if(null == Global.getInstance().getServerUrl() || Global.getInstance().getServerUrl().isEmpty())
                {
                    Global.getInstance().setServerUrl(getServerUrl());
                }
                if(null == Global.getInstance().getUpdateUrl() || Global.getInstance().getUpdateUrl().isEmpty())
                {
                    Global.getInstance().setUpdateUrl(getUpdateUrl());
                }
                if(null == Global.getInstance().getLeapAppName() || Global.getInstance().getLeapAppName().isEmpty())
                {
                    Global.getInstance().setLeapAppName(getLeapAppName());
                }

                final Object o = Global.getInstance().call(server,classType,parameters);
                if(handler != null)
                {
                    handler.post(new Runnable() {
                        @Override
                        public void run()
                        {

                            if(null != onCallFinishListener)
                            {
                                onCallFinishListener.onCallFinish(o);
                            }
                        }
                    });
                }
            }
        }).start();

        return null;
    }


    /**
     * 调用接口完成回调方法
     */
    public interface OnCallFinishListener
    {
        void onCallFinish(Object  entityBean);
    }
}
